//
//  HMNewfeatureViewController.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/11.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HMNewfeatureViewController: UICollectionViewController {
//布局参数
    let layout = UICollectionViewFlowLayout()
    
   
    
    init() {
    super.init(collectionViewLayout: layout)
        
        //设置布局参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

       
        self.collectionView!.registerClass(HMNewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NumberOfNewFeatureItem
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HMNewfeatureCell
    
        
        cell.index = indexPath.item
        return cell
    }
    
    

}


extension HMNewfeatureViewController{
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        
        if index == NumberOfNewFeatureItem - 1 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            
            let cell = collectionView?.cellForItemAtIndexPath(indexPath)as! HMNewfeatureCell
            
            cell.startAnimation()
        }
    }


}

class HMNewfeatureCell: UICollectionViewCell {
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    
    
    
    //监听值得改变
    
    var index: Int = 0 {
        didSet{
        
        let imageName = "new_feature_\(index+1)"
            
//        bkgImageView = UIImageView.init(image: UIImage.init(named: imageName))
            bkgImageView.image = UIImage(named: imageName)
            startBtn.hidden = true

        }
    
    }
    
    func startAnimation(){
    startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) in
                HMPrint("动画完成了")
        }
    
    
    }
    
    
    private func prepareUI() {
    //添加控件
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startBtn)
        
        //添加约束
        bkgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        startBtn.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    
    /**
     *  懒加载
     */
    lazy var bkgImageView:UIImageView = UIImageView(image: UIImage.init(named: "new_feature_1"))
    
    lazy var startBtn:UIButton = {
    let button = UIButton()
        button.setBackgroundImage(UIImage.init(named: "new_feature_finish_button"), forState: .Normal)
        button.setBackgroundImage(UIImage.init(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setTitle("开始体验", forState: .Normal)
        button.sizeToFit()
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.addTarget(self, action: "didClick", forControlEvents: .TouchUpInside)
        return button
        
        
    }()
   @objc private func didClick() {
    
    let controller = HMMainViewController()
    
    //拿到代理对象
    (UIApplication.sharedApplication().delegate as!AppDelegate).switchViewController(controller)
    
    }
}




























